﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

namespace QL_NOI_TRU
{
    class Ketnoi
    {
        
        protected internal SqlConnection  _cnn;
        public SqlCommand  _cmd;
        public SqlDataReader _sqldatareader;

      public Ketnoi()
      {
      }

      public Ketnoi(string cnn)
      {
          _cnn = new SqlConnection(cnn);
          _cmd = new SqlCommand();
          _cmd.Connection = _cnn;
      }
      /// <summary>
      /// 
      /// </summary>
      public void Open()
      {
          try
          {
              if(_cnn.State == ConnectionState.Closed)
              _cnn.Open();
          }
          catch (Exception ex)
          {
#if DEBUG
              MessageBox.Show(ex.Message);
#else
              MessageBox.Show("Loi ket noi...");
#endif

          }
      }
      
      /// <summary>
      /// Truy vấn dữ liệu
      /// </summary>
      /// <param name="cmd">LỆnh SQL  T- SQL</param>
      /// <returns>Du lieu</returns>
      public DataTable ExcuteQuery(string cmd)
      {
          try
          {//
              Open();
              _cmd.CommandText = cmd;

             var r= _cmd.ExecuteReader();
             var tb = new DataTable();
             tb.Load(r);

             return tb;
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
              return null;
          }
      }

      public void ExcuteNonQuery(string cmd)
      {
          try
          {
              Open();
              _cmd.CommandText = cmd;

              var r = _cmd.ExecuteNonQuery();
             

           
          }
          catch (Exception ex)
          {
#if DEBUG
              MessageBox.Show(ex.Message);
#else 
              MessageBox.Show("Lỗi kết nối...");
#endif
          }
      }
       
    }
}
